package com.tchintech.pay.view.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.LocalBroadcastManager;
import android.view.KeyEvent;
import android.view.View;

import com.tchintech.pay.R;

import com.tchintech.pay.delegate.MainActivityDelegate;
import com.tchintech.pay.frame.presenter.ActivityPresenter;

import com.tchintech.pay.utils.ExampleUtil;
import com.tchintech.pay.view.fragment.MainFragment;
import com.tchintech.pay.view.fragment.MyMessageFragment;
import com.tchintech.pay.view.fragment.ShareFragment;
import com.tchintech.pay.widget.DialogWiget;

import java.io.File;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by Administrator on 2017/7/5 0005.
 */

public class MainActivity extends ActivityPresenter<MainActivityDelegate> implements View.OnClickListener {

    private Handler handler;
    private boolean exit = false;
    private DialogWiget dialog = new DialogWiget();
    private MessageReceiver mMessageReceiver;
    public static final String MESSAGE_RECEIVED_ACTION = "com.tchintech.pay.MESSAGE_RECEIVED_ACTION";
    public static final String KEY_TITLE = "title";
    public static final String KEY_MESSAGE = "message";
    public static final String KEY_EXTRAS = "extras";
    //需要打印的pos单数据
    public static String posStr[] = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //主界面禁止侧滑回退
        setSwipeBackEnable(false);
        getSupportFragmentManager().beginTransaction().replace(R.id.main_new_frame, new MainFragment()).commit();
        viewDelegate.clearSelection(0);
        registerMessageReceiver();

    }

    public void registerMessageReceiver() {
        mMessageReceiver = new MessageReceiver();
        IntentFilter filter = new IntentFilter();
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.addAction(MESSAGE_RECEIVED_ACTION);
        LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, filter);
    }

    @Override
    protected void initView() {
        super.initView();
        //首次加载的fragment
        System.out.println("加载fragment");
        viewDelegate.setOnClickListener(this, R.id.main_linear_wallet, R.id.main_linear_share, R.id.main_linear_my);
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                exit = false;
            }
        };
    }

    public class MessageReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                if (MESSAGE_RECEIVED_ACTION.equals(intent.getAction())) {
                    String messge = intent.getStringExtra(KEY_MESSAGE);
                    String extras = intent.getStringExtra(KEY_EXTRAS);
                    StringBuilder showMsg = new StringBuilder();
                    showMsg.append(KEY_MESSAGE + " : " + messge + "\n");
                    if (!ExampleUtil.isEmpty(extras)) {
                        showMsg.append(KEY_EXTRAS + " : " + extras + "\n");
                    }
                    // setCostomMsg(showMsg.toString());
                }
            } catch (Exception e) {
            }
        }
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_linear_wallet:
                getSupportFragmentManager().beginTransaction().replace(R.id.main_new_frame, new MainFragment()).commit();
                viewDelegate.clearSelection(0);
                break;
            case R.id.main_linear_share:
                //分享
                getSupportFragmentManager().beginTransaction().replace(R.id.main_new_frame, new ShareFragment()).commit();
                viewDelegate.clearSelection(1);
                break;
            case R.id.main_linear_my:
                //getSupportFragmentManager().beginTransaction().replace(R.id.main_new_frame, new MyInfoFragment()).commit();
                getSupportFragmentManager().beginTransaction().replace(R.id.main_new_frame, new MyMessageFragment()).commit();
                viewDelegate.clearSelection(2);
                break;
        }
    }


    @Override
    protected Class<MainActivityDelegate> getDelegateClass() {
        return MainActivityDelegate.class;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK == keyCode) {
            if (!exit) {
                exit = true;
                showMsg("再按一次退出应用");
                //利用延迟2秒来达到退出的目的
                handler.sendEmptyMessageDelayed(0, 2000);
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
